package com.dex4d.ui.wallet.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dex4d.R;
import com.dex4d.core.AppToolBarBaseActivity;
import com.dex4d.core.subscribers.ProgressSubscriber;
import com.dex4d.model.User;
import com.dex4d.model.WalletModel;
import com.dex4d.ui.demo.HomeActivity;
import com.dex4d.utils.EthWalletUtil;
import com.dex4d.core.http.DefaultTransformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.functions.Func1;

/**
 * Created by chengyuchun on 2018/10/26.
 */
public class RestoreKeystoreActivity extends AppToolBarBaseActivity {
    @BindView(R.id.et_keystore)
    EditText mEtKeystore;

    @BindView(R.id.et_password)
    EditText mEtPassword;

    @BindView(R.id.btn_finish)
    Button mBtnFinish;

    @Override
    protected void initToolbar() {
        showBackView();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_restore_keystore;
    }

    @Override
    protected void initVariables() {

        TextWatcher textWatcher = new TextWatcher(){
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(mEtKeystore.getText().length() >0 && mEtPassword.getText().length()>0){
                    mBtnFinish.setBackgroundResource(R.drawable.btn_blue_clickable_bg);
                    mBtnFinish.setTextColor(getResources().getColor(R.color.white));
                    mBtnFinish.setClickable(true);
                }else {
                    mBtnFinish.setBackgroundResource(R.drawable.btn_blue_unclick_bg);
                    mBtnFinish.setTextColor(getResources().getColor(R.color.gray_B7C5CA));
                    mBtnFinish.setClickable(false);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };

        mEtKeystore.addTextChangedListener(textWatcher);
        mEtPassword.addTextChangedListener(textWatcher);
    }

    @Override
    protected void loadData() {

    }

    @OnClick(R.id.btn_finish)
    public void onViewClick(){
        List<String> paramList = new ArrayList<String>();
        paramList.add(mEtKeystore.getText().toString());
        paramList.add(mEtPassword.getText().toString());

        Observable.just(paramList)
                .map(new Func1<List<String>, WalletModel>() {
                    @Override
                    public WalletModel call(List<String> paramList) {
                        //用keystore和密码恢复钱包
                        return EthWalletUtil.restoreKeystore(paramList.get(0),paramList.get(1));
                    }
                })
                .map(new Func1<WalletModel, Boolean>() {
                    @Override
                    public Boolean call(WalletModel walletModel) {
                        //保存钱包
                        if(walletModel == null){
                            return false;
                        }else {
                            User.getInstance().setAddress(walletModel.address);
                            User.getInstance().setWalletName(walletModel.name);
                            User.getInstance().setKeystore(walletModel.keystore);
                            User.getInstance().setPassphrase(walletModel.passphrase);
                            User.getInstance().setBackup(true);
                            User.getInstance().save();

                            return true;
                        }
                    }
                })
                //这是线程切换，切换成主线程，异步过程中只要有网络异常或返回错误，即会回调onError方法
                .compose(new DefaultTransformer<Boolean>())
                .subscribe(new ProgressSubscriber<Boolean>(this, true) {
                    @Override
                    public void onNext(Boolean flag) {
                        if(flag){
                            //Toast.makeText(RestoreKeystoreActivity.this,"success",Toast.LENGTH_SHORT).show();
                            openActivity(HomeActivity.class);
                            finish();
                        }else {
                            Toast.makeText(RestoreKeystoreActivity.this,getString(R.string.restore_failure),Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                });
    }
}
